{extend name='/public/base' /}

{block name='content'}
<div class="layui-card">
    <div class="layui-card-body">
        <table class="layui-hide" id="test" lay-filter="test"></table>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添 加</button>
        <button class="layui-btn layui-btn-sm" lay-event="allDel">删 除</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="statusDome">
    {{#  if(d.status == 1 ){ }}
    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="disable">正常</a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs" lay-event="enable">已禁用</a>
    {{#  } }}
</script>

<form id="form" class="layui-form layui-form-pane" style="display: none;width: 80%;margin-top: 20px" lay-filter="form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input type="text" name="account" lay-verify="required" placeholder="账号" autocomplete="off"
                       class="layui-input username">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">登录密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="required" placeholder="登录密码" autocomplete="off"
                       class="layui-input password">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">账号状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="正常" lay-verify="required">
                <input type="radio" name="status" value="0" title="禁用" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色组</label>
            <div class="layui-input-block">
                <select name="role_id" lay-verify="required" lay-search="">
                    <option value="">请选择</option>
                    {foreach data as $v}
                    <option value="{$v.id}">{$v.name}</option>
                    {/foreach}
                </select>
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="number" name="mobile" lay-verify="required" placeholder="手机号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
    </div>
</form>
{/block}

{block name='js'}
<script>
    layui.use(['table', 'form', 'layer'], function () {
        let $ = layui.$,
            form = layui.form,
            layer = layui.layer,
            table = layui.table,
            TableData = table.render({
                id: 'TableData',
                elem: '#test',
                toolbar: '#toolbarDemo',
                page: true,
                method: 'post',
                url: '/manage/system/admins/getList', //数据接口
                title: '用户数据表',
                cols: [[ //表头
                    {type: 'checkbox'},
                    {field: 'id', title: 'id', hide: true},
                    {field: 'account', title: '账号', minWidth: 200, align: 'center'},
                    {field: 'mobile', title: '手机号', minWidth: 150, align: 'center'},
                    {field: 'status', title: '状态', minWidth: 150, align: 'center', toolbar: '#statusDome'},
                    {field: 'createTime', title: '注册时间', width: 180, align: 'center'},
                    {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
                ]]
            });

        //监听搜索
        // form.on('submit(LAY-user-front-search)', function (data) {
        //     let field = data.field;
        //     //执行重载
        //     table.reload('LAY-user-manage', {
        //         where: field
        //     });
        // });

        //工具栏事件
        table.on('toolbar(test)', function (obj) {
            switch (obj.event) {
                case 'add':  //添加
                    addData();
                    break;
                case 'allDel':  //多选删除
                    let checkStatus = table.checkStatus(obj.config.id);
                    let data = checkStatus.data;
                    delData(data);
                    break;
            }
        });
        //监听工具条
        table.on('tool(test)', function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case 'edit':
                    editData(data);
                    break;
                case 'del':
                    delData([data]);
                    break;
            }
        });

        //添加
        function addData() {
            $('#form')[0].reset();
            layer.open({
                type: 1,
                title: '添加管理员',
                content: $("#form"),
                area: ['500px', '450px'],
                btn: ['确定', '取消'],
                btn1: function (index, layero) {
                    let data = form.val('form');
                    $.ajax({
                        method: 'post',
                        data: data,
                        url: '/manage/system/admins/add',
                        success: function (res) {
                            if (res.code !== 0) {
                                layer.msg(res.message);
                            } else {
                                layer.msg('添加成功');
                                layer.close(index);
                                TableData.reload();
                            }
                        },
                        error: function (res) {

                        }
                    })
                }
                , btn2: function (index, layero) {
                    $('#form')[0].reset();
                    layer.close(index);
                    return false;
                }
            });
        }

        //修改
        function editData(arr) {
            form.val('form', {
                'account': arr.account,
                'status': arr.status,
                'role_id': arr.roleId,
                'mobile': arr.mobile,
            });
            layer.open({
                type: 1,
                title: '修改管理员',
                content: $("#form"),
                area: ['500px', '450px'],
                btn: ['确定', '取消'],
                btn1: function (index, layero) {
                    let data = form.val('form');
                    data['id'] = arr.id;
                    $.ajax({
                        method: 'post',
                        data: data,
                        url: '/manage/system/admins/update',
                        success: function (res) {
                            if (res.code !== 0) {
                                layer.msg(res.message);
                            } else {
                                layer.msg('修改成功');
                                layer.close(index);
                                TableData.reload();
                            }
                        },
                    })
                },
                btn2: function (index, layero) {
                    layer.close(index);
                    return false;
                }
            });
        }

        //删除
        function delData(arr) {
            if (arr.length < 1) {
                layer.msg('请选择要删除的数据');
                return false;
            }
            layer.confirm('确定删除这些数据吗？', function (index) {
                $.post('/manage/system/admins/delete',
                    {data: arr},
                    function (res) {
                        if (res.code === 0) {
                            layer.msg('删除成功')
                            TableData.reload();
                        } else {
                            layer.msg(res.message);
                        }
                    }
                );
            })
        }
    });
</script>
{/block}